package com.snails.app.base;

import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatActivity;

import com.gyf.immersionbar.ImmersionBar;
import com.snails.app.R;
import com.snails.app.utils.ActivityHelper;
import com.snails.app.utils.ToastUtil;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author lawrence
 * @date 2019-04-26 18:28
 */
public abstract class BaseActivity extends AppCompatActivity {
    // ----------------------------------------------------------------------------
    // ----------- abstract ------------
    // 继承类需实现方法
    // ----------------------------------------------------------------------------
    // 通过资源，设置布局
    @LayoutRes protected abstract int getLayoutId();
    // 通过创建View对象，设置布局（优先级高于getLayoutId）
    @Nullable protected abstract View getRootView();

    protected abstract void onViewCreated(@Nullable Bundle savedInstanceState);
    protected abstract void initToolbar();
    protected abstract void initEventAndData();
    protected abstract void onDetach();

    // ----------------------------------------------------------------------------
    // ----------- ------------
    // ----------------------------------------------------------------------------
    private Unbinder unBinder;
    protected BaseActivity mCtx = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContext().setRootView();
        unBinder = ButterKnife.bind(this);
        if (isImmersionBarEnabled()) initImmersionBar();
        onViewCreated(savedInstanceState);
        initToolbar();
        initEventAndData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        onDetach();
        if (unBinder != null && unBinder != Unbinder.EMPTY) {
            unBinder.unbind();
        }
        unBinder = null;
    }

    @Override
    public void startActivity(Intent intent) {
        ActivityHelper.startAnim(mCtx, () -> super.startActivity(intent));
    }

    @Override
    public void startActivity(Intent intent, @Nullable Bundle options) {
        ActivityHelper.startAnim(mCtx, () -> super.startActivity(intent, options));
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        ActivityHelper.startAnim(mCtx, () -> super.startActivityForResult(intent, requestCode));
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode, @Nullable Bundle options) {
        ActivityHelper.startAnim(mCtx, () -> super.startActivityForResult(intent, requestCode, options));
    }

    @Override
    public void finish() {
        ActivityHelper.finishAnim(mCtx, () -> super.finish());
    }

    @Override
    public void finishActivity(int requestCode) {
        ActivityHelper.finishAnim(mCtx, () -> super.finishActivity(requestCode));
    }

    protected String $s(@StringRes int resId) {
        return getString(resId);
    }

    protected String $s(@StringRes int resId, Object... formatArgs) {
        return getString(resId, formatArgs);
    }

    private BaseActivity setContext() {
        return this.mCtx = this;
    }

    /**
     * 是否可以使用沉浸式状态栏
     */
    protected boolean isImmersionBarEnabled() {
        return true;
    }

    /**
     * 默认沉浸式状态栏颜色
     */
    protected void initImmersionBar() {
        //设置共同沉浸式样式
        ImmersionBar.with(this).navigationBarColor(R.color.defaultStatusBarColor).init();
    }

    private BaseActivity setRootView() {
        final View rootView = getRootView();
        if (rootView != null) {
            setContentView(rootView);
        } else {
            setContentView(getLayoutId());
        }
        return this;
    }

    public void showLongToast(String message) {
        ToastUtil.instance().showText(message, Toast.LENGTH_LONG, Gravity.CENTER);
    }

    public void showLongToast(@StringRes int resId) {
        ToastUtil.instance().showText(resId, Toast.LENGTH_LONG, Gravity.CENTER);
    }


}
